<template>
  <div class="shop">
    <div class="shop-info">
      <p class="name">{{ shop.name }}</p>
      <p class="point">商店评分: {{ shop.point }}</p>
    </div>
    <div class="food-list">
      <div class="food-item" v-for="i in shop.items" :key="i.id">
        <div class="img-box">
          <img :src="i.url" alt="" />
        </div>
        <div class="food-info">
          <p class="food-name">
            {{ i.name }} <span class="price">￥{{ i.price }}</span>
          </p>
          <p class="sales">已售: {{ i.sales }} 份</p>
        </div>

        <button @click="addToOrder(i)">立即加入订单</button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRoute } from "vue-router";
import { Shop, Item, getShop } from "../interface";
import { ordersStore } from "../store";

const store = ordersStore();
const route = useRoute();

let shop: Shop = getShop(Number(route.params.id)) as Shop;

// 将商品加入订单列表中
function addToOrder(food: Item) {
  if (store.orders.includes(food)) {
    alert("该商品已经在订单中了！");
    return;
  }
  //添加商品到订单的pinia中
  store.orders.push(food);
  alert("成功加入订单！");
}
</script>

<style scoped>
.shop {
  padding: 20px 10px;
}
.shop-info {
  display: flex;
  justify-content: space-between;
}
.food-item {
  box-shadow: 0 0 5px #ccc;
  margin-bottom: 10px;
  padding: 10px;
}
.img-box {
  overflow: hidden;
}
.img-box img {
  width: 100%;
  transition: all 0.5s;
}

.img-box img:hover {
  transform: scale(1.2);
}

.food-info {
  display: flex;
  justify-content: space-between;
  line-height: 30px;
}

.food-info .price {
  font-weight: 500;
  font-size: 22px;
  color: #ff6600;
}

.food-info .sales {
  color: #999999;
}
</style>
